//ImageHandling.cpp
// compile with: /EHsc /LD
#include "Including.h"

using namespace std;

namespace SDLFuncs
{

	///////////////////////////////////
	/* Sets up a texture
	/* Receives the file name from the input and assigns it to GLuint&
	*/
	void MySDLFuncs::SetupTexture(string imageName, GLuint &texture)
	{
		GLenum texture_format;
		SDL_Surface *image = NULL;

		//load the image
		if( (image = LoadImage(imageName.c_str())))
		{
			//The number of bytes used to represent each pixel in a surface
			int noOfColors = image->format->BytesPerPixel;
			
			//Has Alpha
			if(noOfColors == 4)	
			{
				//Binary mask used to retrieve individual color values
				//0x000000ff is red
				if (image->format->Rmask == 0x000000ff)
					texture_format = GL_RGBA;
				else
					texture_format = GL_BGRA;
			} 
			// Has no alpha channel
			else if (noOfColors == 3)     
			{
				if (image->format->Rmask == 0x000000ff)
					texture_format = GL_RGB;
				else
					texture_format = GL_BGR;
			}
			else
				cout << "Loading Failed:" << imageName << endl;

			//Generate OpenGL Image Handle
			glGenTextures(1, &texture);
			//Bind the texture object
			glBindTexture(GL_TEXTURE_2D, texture);
			//Set texture stretching properties,the texture minifying and magnification using linear
			//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
			//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
		    //glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
			//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			//Define texture image, specify a two-dimensional texture image
			//glTexImage2D(GL_TEXTURE_2D, 0, noOfColors, image->w, image->h, 0, texture_format, GL_UNSIGNED_BYTE, image->pixels);
			gluBuild2DMipmaps(GL_TEXTURE_2D, noOfColors, image->w, image->h, texture_format, GL_UNSIGNED_BYTE, image->pixels);
		}
		else
			cout << "Loading Failed with image: " << imageName << endl;

		SDL_FreeSurface(image);
	}



	///////////////////////////////////
	/* Loads an image from a file
	*/
	SDL_Surface* MySDLFuncs::LoadImage(string filename)
	{
			//SDL_Surface represents an area of graphical memory that can be drawn to
			SDL_Surface *loadedImage = NULL;
			SDL_Surface *optimizedImage = NULL;
			loadedImage = IMG_Load(filename.c_str());

		if(loadedImage == NULL)
		{
			cout << "Loading Image Failed: " << filename << endl;
			return NULL;
		}
		else{
			//Convert a surface to the display format. It takes a surface and copies it 
			//to a new surface of the pixel format and colors of the video framebuffer 
			//plus an alpha channel, suitable for fast blitting onto the display surface.
			optimizedImage = SDL_DisplayFormatAlpha(loadedImage);
			//Frees(deletes) surface
			SDL_FreeSurface(loadedImage);
		}
		return optimizedImage;
	}


}